<?php

class mailTaskActions extends automailTaskActions
{
	/**
	 * send first mails from list with limit (first vith high priority)
	 */
    public function executeSendMailTasks()
    {
    	$start = 'Corn_starts_send_mailtasks';
    	SymConfig::set($start, time());
    	$c = new Criteria();
    	$c->add(MailTaskPeer::STATUS_ID, MyConstants::STATUS_NEW);
    	$c->addDescendingOrderByColumn(MailTaskPeer::PRIORITY);
    	$c->addDescendingOrderByColumn(MailTaskPeer::ID);
    	$c->setLimit(MailTask::LIMIT_SENDING_MAILS_PER_TIME);
    	$tasks = MailTaskPeer::doSelect($c);
    	if(empty($tasks))
    	{
    		$start_empty = 'Corn_starts_send_mailtasks_empty';
    	   	SymConfig::set($start_empty, time());
    	}
    	$count = 0;
    	foreach( $tasks as $task )
    	{
    		$count++;
    		/* @var $task MailTask */
    	    if( !$task->getMailto() )
            {
                continue;
            }
            $senderMail = $task->getMailfrom();
            $senderName = $task->getSender();
            $recipient = $task->getMailto();
            $subject = $task->getSubject();
            $body = $task->getBody();
            
            $mail = new sfMail();
            $mail->initialize();
            $mail->addCustomHeader('MIME-Version', '1.0');
            $mail->addCustomHeader('Content-type', 'text/html;charset=utf-8');
    
            $mail->setCharset('utf-8');
    
            // definition of the required parameters
            $mail->setSender($senderMail, $senderName);
            $mail->setFrom($senderMail, $senderName);
    
            $mail->addAddress($recipient);
    
            $mail->setSubject($subject);
            $mail->setBody($body);
            
            $mail->send();
    
            $task->setStatusId(MyConstants::STATUS_APPROVED);
            $task->setSentAt(time());
            
            $task->save();
            $end = 'Cron_sends_mails_end';
            SymConfig::set($end, time());
    	}
    	$countLast = 'coun_Cron_last_sended_mails';
    	SymConfig::set($countLast, $count);
    	return $this->redirect('mailtask/list');
    	
    }
    /**
     * 
     * Create mailtasks for selected users
     */
    public function executeBulktasks()
    {
    	$webRequest = new sfWebRequest();
      	$host = $webRequest->getHost();
    	if( $this->getRequest()->getMethod() == sfRequest::POST )
    	{
    		$subject = $host;
    		if($this->getRequestParameter('subject') && ($this->getRequestParameter('subject')!=''))
    		{
    			$subject = $this->getRequestParameter('subject');
    		}
    		$priority = MailTask::PRIORITY_NORMAL;
    	    if($this->getRequestParameter('priority') && ($this->getRequestParameter('priority')!=''))
            {
                $priority = $this->getRequestParameter('subject');
            }
    		$c = new Criteria();
    		if( $this->getRequestParameter('users') )
    		{
    			foreach($this->getRequestParameter('users') as $role )
    			{
    			 $c->add(UserPeer::ROLES, UserPeer::ROLES . ' REGEXP "[[:<:]]' . $role . '[[:>:]]"', Criteria::CUSTOM);
    			}
    		}
            $users = UserPeer::doSelect($c);
    		/* @var $user User */
            if(($this->getRequestParameter('body'))&&($this->getRequestParameter('body') != ''))
            {
	    		$body = $this->getRequestParameter('body');
	    		foreach( $users as $user )
	    		{
	    			$bodyNew = preg_replace('/[%]([A-Za-z0-9_]+)[%]/', $user->getNick(), $body);
	    			$mailto = $user->getEmail();
	    			$title = $subject;
	    			$sender = $host;
	    			$mailfrom = sfConfig::get('app_webmaster_mail');
	    			MailTask::createNew(MyConstants::UNDEFINED_ID, $priority, $subject, $mailto, $title, $bodyNew, $sender, $mailfrom);
	    		}
            }
            $this->setFlash('status', MyConstants::STATUS_APPROVED);
    	}
    	
    }

}
